import java.util.Scanner;

/**
 * 实现一个简易银行账户管理系统，要求：
 * 1. 使用数组存储多个账户的余额
 * 2. 实现存款、取款和转账功能
 * 3. 取款时检查余额是否充足
 * 4. 统计所有账户的总余额
 * 5. 找出余额最多和最少的账户
 */
public class BankAccountManager {
    //1.用数组存储多个账户的余额
    static Scanner sc = new Scanner(System.in);
    static String[] password = {"123", "666", "444", "888", "222", "777", "789"};
    static double[] balance = {4000, 1314520.75, 0, 100, 888.5, 66666.5, 19999};

    public static void main(String[] args) {

        int error = 0;
        boolean flag = true;

        //4.统计所有账户的总余额，找出余额最多的账户与余额最少的账户
        System.out.println();
        double maxb = balance[0];
        double minb = balance[0];
        double sumb = 0;
        int max_id = 0;
        int min_id = 0;

        for (int i = 0; i <= 6; i++) {
            sumb += balance[i];
            if (balance[i] > maxb) {
                maxb = balance[i];
                max_id = i;
            }
            if (balance[i] < minb) {
                minb = balance[i];
                min_id = i;
            }
        }
        System.out.println("所有账户的总余额为：" + sumb + "元 余额最多的账户为：" + (max_id + 1) + "号账户 余额最少的账户为：" + (min_id + 1) + "号账户");

        //2.实现用户登录功能，并能够实现存款，取款，转账功能
        while (flag) {
            System.out.print("请输入您的用户id：");
            int id = sc.nextInt();
            System.out.print("请输入您的密码：");
            String psw = sc.next();

            int i;
            for (i = 0; i <= 6; i++) {
                if ((i + 1) == id) {
                    if (psw.equals(password[i])) {
                        System.out.println("您的账户有：" + balance[i] + "元");

                        System.out.println("请输入您要执行的操作编号：1.存款，2.取款，3.转账");

                        int op = sc.nextInt();
                        if (op == 1) {
                            putMoney(id);
                            flag = false;
                            break;
                        }
                        if (op == 2) {
                            getMoney(id);
                            flag = false;
                            break;
                        }
                        if (op == 3) {
                            giveMoney(id);
                            flag = false;
                            break;
                        }

                    } else {
                        error += 1;
                        System.out.println("用户id或密码输入错误，请重新输入，您还有" + (6 - error) + "次机会！！");
                        break;
                    }
                }
            }
            if (i == 7) {
                error += 1;
                System.out.println("用户id或密码输入错误，请重新输入，您还有" + (6 - error) + "次机会！！");
            }

            if (error == 6) {
                System.out.print("由于多次输入错误您的账户已被冻结!");
                break;
            }
        }
    }

    /**
     * 账户取款方法
     *
     * @param id
     */
    public static void getMoney(int id) {

        System.out.print("请输入您的取款金额：");
        double n = sc.nextDouble();
        //3. 取款时检查余额是否充足
        if (balance[id - 1] < n) {
            System.out.print("您的账户余额不足！！");
        } else {
            balance[id - 1] -= n;
            System.out.println("您的账户还剩：" + balance[id - 1] + "元");
        }
    }

    /**
     * 账户存款方法
     *
     * @param id
     */
    public static void putMoney(int id) {

        System.out.print("请输入您的存款金额：");
        double n = sc.nextDouble();
        balance[id - 1] += n;
        System.out.println("已为您存入" + n + "元，您的账户现有：" + balance[id - 1] + "元");

    }

    /**
     * 账户转账方法
     *
     * @param id
     */
    public static void giveMoney(int id) {

        System.out.print("请输入您要转账的用户id：");
        int id2 = sc.nextInt();

        System.out.print("请输入您要转账的金额：");
        double n = sc.nextDouble();
        if (balance[id - 1] < n) {
            System.out.print("您的账户余额不足！！");
        } else {
            balance[id - 1] -= n;
            balance[id2 - 1] += n;
            System.out.print("您的账户还剩：" + balance[id - 1] + "元，" + "已为" + id2 + "号账户转账" + n + "元。");
        }

    }
}